#pragma once
#include<iostream>
#include<list>
#include"iterator.h"
using namespace std;
namespace mutou {
	template <class T>
	struct List_Node
	{
		List_Node* prev;
		List_Node* next;
		T data;
		List_Node(const T& val = T())
			:prev(nullptr)
			, next(nullptr)
			, data(val)
		{
		}
	};
	template <class T ,class Ref,class Ptr>
	struct list_iterator
	{
		typedef List_Node<T> Node;
		typedef list_iterator<T,Ref,Ptr> self;
		Node* node;
		list_iterator( Node* val)
			:node(val)
		{
		}
		Ptr operator->()
		{
			return &node->data;
		}
		Ref operator*()
		{
			return node->data;
		}
		self& operator++()
		{
			node = node->next;
			return *this;
		}
		self operator++(int)
		{
			self tmp(*this);
			node = node->next;
			return tmp;
		}
		self& operator--()
		{
			node = node->prev;
			return *this;
		}
		self operator--(int)
		{
			self tmp(*this);
			node = node->prev;
			return tmp;
		}
		bool operator!=(const self& it)
		{
			return node != it.node;
		}
		bool operator==(const self& it)
		{
			return node == it.node;
		}


	};
	template <class T>
	class list
	{
		typedef List_Node<T> Node;
			
	public:
	
		typedef list_iterator<T, T&,T*> iterator;

	//	typedef iterator<T, const T&> const_iterator;
		typedef list_iterator<T,const T&,const T*>  const_iterator;

	/*	typedef iterator<T, const T&> const_iterator;*/
		typedef Reverse_iterator<iterator, T&, T*> reverse_iterator;
		typedef Reverse_iterator<const_iterator, const T&, const T*> const_reverse_iterator;

		void empty()
		{

			head = new Node;
			head->next = head;
			head->prev = head;
			_size = 0;
		}
		list()
		{
			empty();
		}
		const_reverse_iterator crbegin() const
		{
			return const_reverse_iterator(end());
		}
		const_reverse_iterator crend() const
		{
			return const_reverse_iterator(begin());
		}

		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}
		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}
		const_iterator begin() const
		{
			return const_iterator(head->next);
		}
		const_iterator end() const
		{
			return const_iterator(head);
		}

		iterator begin()
		{
			return iterator(head->next);
		}
		iterator end()
		{
			return iterator(head);
		}
		list(const list<T>& it)
		{
			empty();
			for (auto& ch : it)
			{
				push_back(ch);
			}
		}
		list(initializer_list<T> it)
		{
			empty();
			for (auto& ch : it)
			{
				push_back(ch);
			}
		}
		void swap(const list<T>& it)
		{
			std::swap(head, it.head);
			std::swap(size, it.size);
		}

		~list()
		{
			clear();
			delete head;
			head = nullptr;
			cout << "~list" << endl;
		}
		list<T>& operator=(list<T> val)
		{
			swap(val);
			return *this;
		}
		
		void clear()
		{
			iterator cur = begin();
			while (cur != end())
			{
				cur = erase(cur);
			}
		}
		const size_t size()const
		{
			return _size;
		}
		void push_back(const T& x)
		{
			/*Node* tail = head->prev;
			Node* newnode = new Node(x);
			tail->next = newnode;
			newnode->prev = tail;
			newnode->next = head;
			head->prev = newnode;*/
			insert(end(), x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void pop_front()
		{
			erase(begin());
		}
		void pop_back()
		{
			erase(--end());
		}
		void insert(iterator pos, const T& val)
		{
			//prev newnode  cur 
			Node* prev = pos.node->prev;
			Node* cur = pos.node;
			Node* newnode = new Node(val);
			prev->next = newnode;
			cur->prev = newnode;
			newnode->prev = prev;
			newnode->next = cur;
			++_size;
		}
		iterator erase(iterator pos)
		{
			//prev pos next
			Node* prev = pos.node->prev;
			Node* next = pos.node->next;
			Node* cur = pos.node;
			prev->next = next;
			next->prev = prev;
			delete cur;
			--_size;
			//return next;
			return iterator(next);
		}
	private:
		Node* head;
		size_t _size;

	};
	
}